<template>
  <el-dropdown trigger="click">
    <div class="avatar">
      <img src="@/assets/images/avatar.gif" alt="avatar" />
    </div>
    <template #dropdown>
      <el-dropdown-menu width="100px">
        <el-dropdown-item @click="logout">
          <el-icon><SwitchButton /></el-icon>退出登录
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
  <EditDateDrawer ref="editDateRef" />
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import { useUserStore } from "@/stores/modules/user";
import { ElMessageBox } from "element-plus";
import EditDateDrawer from "./EditDateDrawer.vue";
import { LOGIN_URL } from "@/config";
import { useKeepAliveStore, useTabsStore } from "@/stores";

defineOptions({
  name: "Avatar"
});

const router = useRouter();
const useUser = useUserStore();
const useTabs = useTabsStore();
const useKeepAlive = useKeepAliveStore();

// 退出登录
const logout = () => {
  ElMessageBox.confirm("您是否确认退出登录?", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    useTabs.closeMultipleTab();
    useKeepAlive.setKeepAliveName();
    router.replace(LOGIN_URL);
    useUser.clearCacheHandler();
  });
};

const editDateRef = ref<InstanceType<typeof EditDateDrawer> | null>(null);
</script>

<style scoped lang="scss">
.avatar {
  width: 40px;
  height: 40px;
  overflow: hidden;
  cursor: pointer;
  border-radius: 50%;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>
